package com.cqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqh.common.CustomException;
import com.cqh.entity.Category;
import com.cqh.entity.Dish;
import com.cqh.entity.Setmeal;
import com.cqh.mapper.CategoryMapper;
import com.cqh.service.CategotyService;
import com.cqh.service.DishService;
import com.cqh.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategotyService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据Id删除分类，删除之前需要判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count=dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联了菜品，如果关联抛出异常
        if(count>0){
            throw new CustomException("当前分类下关联了菜品不能删除");
        }
        //查询当前分类是否关联套餐，如果关联抛出异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1=setmealService.count(setmealLambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("当前分类下关联了套餐不能删除");
        }

        super.removeById(id);

    }
}
